Exercise (Week 2)
Table of Contents
DUE: Thu June 23 09:10:00
1 Getting Started
Before you begin, make sure that you installed Haskell according to the
Haskell Setup instructions. For this and next week's exercises, you
might find it helpful to have a look at the first six chapters of
Learning Haskell, as the ShapeGraphics
library you'll be using is quite similar to the graphics library presented there.
To get started, follow the instructions below.
Download the exercise tarball and extract it to a directory in your home directory at CSE.
This tarball contains a file, called Ex01.hs
, wherein you will do all of your programming.
To test your code, run the following shell commands to open a GHCi session:
$ 3141
newclass starting new subshell for class COMP3141...
$ cabal repl
Resolving dependencies...
Configuring Ex01-1.0...
Preprocessing executable 'Ex01' for Ex01-1.0..
GHCi, version 8.2.2: http://www.haskell.org/ghc/ :? for help
[1 of 2] Compiling ShapeGraphics (ShapeGraphics.hs, interpreted)
[2 of 2] Compiling Ex01 (Ex01.hs, interpreted)
Ok, two modules loaded.
*ShapeGraphics> :l Ex01.hs
[1 of 2] Compiling ShapeGraphics (ShapeGraphics.hs, interpreted)
[2 of 2] Compiling Ex01 (Ex01.hs, interpreted)
Ok, two modules loaded.
*Ex01> writeToFile housePic
(Ignore the -Wmissing-home-modules
warning if you get one.)
Calling writeToFile
as demonstrated above will write the provided picture to a file called ex01.png
in the directory in which you invoked GHCi.
Note that you will only need to submit Ex01.hs
, so only make changes to that file.
Download the exercise tarball and extract it to a directory on your local machine.
This tarball contains a file, called Ex01.hs
, wherein you will do all of your programming.
To test your code, run the following shell commands to open a GHCi session:
$ stack repl
Configuring GHCi with the following packages: Ex01
Using main module: 1. Package 'Ex01' component exe:Ex01 ...
GHCi, version 8.2.2: http://www.haskell.org/ghc/ :? for help
[1 of 2] Compiling ShapeGraphics (ShapeGraphics.hs, interpreted)
[2 of 2] Compiling Ex01 (Ex01.hs, interpreted)
Ok, two modules loaded.
*Ex01 ShapeGraphics> writeToFile housePic
Calling writeToFile
as demonstrated above will write the provided picture (in this case housePic
) to a file called ex01.png
in the directory in which you invoked GHCi.
Note that you will only need to submit Ex01.hs
, so only make changes to that file.
As a first step, read through the comments in Ex01.hs
, and look at the definition of the
various data types used there.
You can safely ignore the definition of the function drawPicture
.
All you need to know is that it takes as first parameter a floating point value which sets
the line width of the picture, and as second parameter a Picture
(a list of
PictureObject
values), and returns a .png
image of the picture.
The function writeToFile
further saves the resulting image to disk as ex01.png
.
2 Part 1: Simple Picture (3 Marks)
Your first task is to complete the definition of four functions, housePic
, merge
, window
and chimneyHouse
.
Complete the definition of picture housePic
in Ex01.hs
such that it is a list of
two picture objects: house
and door
. The house
object is the outline of a house, given as a Path
.
The house
should be coloured green
, and have a solid line style. The Path
making up the outline
consists of multiple points: the coordinates of these points are given in the lists houseCOx
and houseCOy
.
Since the ShapeGraphics
library expects the coordinates in a different format, you will have to define
a conversion function merge :: [Float] -> [Float] -> [Point]
which converts the two lists of coordinates
into the expected type.
The door
object should be a path defined by the x,y
coordinate pairs given in doorCOs
, coloured red
, and
with solid line style.
Use writeToFile housePic
(as demonstrated with example
above) to test your definition.
As a next step, define a new picture chimneyHouse
, which additionally has a chimney and a window, so
that it looks as follows:
The additional four coordinates needed for the chimney are provided in chimneyCOs
. You will have to
combine these with the house outline.
The window should be a rectangle with the coordinates given in windowCOs
, filled with the cyan
colour defined in Ex01.hs
.
Important: A complete solution of Part 1 must have working definitions of
housePic
, merge
, window
and chimneyHouse
. Make sure to use exactly the given coordinates;
otherwise, automarking will not give you any marks.
3 Part 2: Moving Objects (3 Marks)
You are given a function which moves a point along a given vector and returns the new point. Use this function to complete the definition of the function:
movePictureObject :: Vector -> PictureObject -> PictureObject
which moves a picture object. You need to provide a rule for every possible PictureObject
(the pattern matching for Path
is already part of the given definition).
The following GHCi session
GHCi, version 8.2.2: http://www.haskell.org/ghc/ :? for help [1 of 2] Compiling ShapeGraphics (ShapeGraphics.hs, interpreted) [2 of 2] Compiling Ex01 (Ex01.hs, interpreted) Ok, two modules loaded. *ShapeGraphics> let myRed = red { opacityC = 180 } *ShapeGraphics> let xy = (Point 400 400) *ShapeGraphics> let circ = Circle xy 100 myRed Solid SolidFill *ShapeGraphics> let v = (Vector 100 100) *ShapeGraphics> writeToFile [circ, movePictureObject v circ]
ought to produce the following image in ex01.png
:
Hint: Path
and Polygon
are the more complicated cases. Don't forget that you can use Hoogle! Haskell might have pre-defined function that help with your task.
4 Part 3: Generating a Picture (3 Marks)
Write a function simpleCirclePic :: Colour -> Float -> Picture
,
such that simpleCirclePic col n
for a positive floating point number n
generates the
picture consisting of overlapping circles:
[Circle (Point 400 400) (1 * (400/n)) col Solid SolidFill, Circle (Point 400 400) (2 * (400/n)) col Solid SolidFill, Circle (Point 400 400) (3 * (400/n)) col Solid SolidFill, .... Circle (Point 400 400) ((n-1) * (400/n)) col Solid SolidFill, Circle (Point 400 400) (n * (400/n)) col Solid SolidFill]
To test it, the following GHCi session
GHCi, version 8.2.2: http://www.haskell.org/ghc/ :? for help [1 of 2] Compiling ShapeGraphics (ShapeGraphics.hs, interpreted) [2 of 2] Compiling Ex01 (Ex01.hs, interpreted) Ok, two modules loaded. *ShapeGraphics> let myPurp = Colour 153 0 153 100 *ShapeGraphics> writeToFile $ simpleCirclePic myPurp 5
ought to produce the following image in ex01.png
:
Hint: You can define this function using explicit recursion, but it is much easier to use the map
function
we discussed in the lecture, and the function enumFromThenTo
. The expression enumFromThenTo start next upperBound
generates the list [start, next, start + 2 * (next - start),..]
up to upperBound
.
5 Submission Instructions
While connected/using a CSE machine, type the following to submit your work from the directory within the Ex01
folder:
$ give cs3141 ex01 Ex01.hs
Alternatively, if the above does not work, try the give web interface.
Note that you will submit only the Haskell module file called Ex01.hs
(and not the entire project).
All work submitted for assessment must be entirely your own. Unacknowledged copying of material, in whole or part, is a serious offence. Before submitting any work you should read and understand the UNSW Plagiarism Policy.